package com.example.a33mvvm.liu;

import static com.tencent.live2.V2TXLiveCode.V2TXLIVE_ERROR_INVALID_LICENSE;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.example.a33mvvm.R;
import com.example.a33mvvm.databinding.ActivityLiuBinding;
import com.example.base_mvvm.base.BaseViewModel;
import com.example.base_mvvm.base.view.BaseActivity;
import com.example.base_mvvm.base.view.BaseView;
import com.example.base_mvvm.entity.PushEntity;
import com.example.base_mvvm.entity.StopPushEntity;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePlayer;
import com.tencent.live2.V2TXLivePusher;
import com.tencent.live2.impl.V2TXLivePlayerImpl;
import com.tencent.live2.impl.V2TXLivePusherImpl;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class LiuActivity extends BaseView<ActivityLiuBinding,PushViewModel> {
    private V2TXLivePusher mLivePusher;
    private TXCloudVideoView mView;
    private V2TXLivePlayer mLivePlayer;
    String url = "	rtmp://196793.push.tlivecloud.com/live/awd?txSecret=951a46f71294066a48b016293c0fa5bf&txTime=65EFC81E";
    @Override
    protected void initData() {
        viewModel.success_start.observe(this, new Observer<PushEntity>() {
            @Override
            public void onChanged(PushEntity pushEntity) {
                if(pushEntity.getCode() == 200){
                   mLivePlayer.startLivePlay(url);
                }else {
                    ToastUtils.showLong(pushEntity.getMessage());
                }


            }
        });
        viewModel.failure_start.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Log.i("TAG", "onChanged: 开播失败"+s);
            }
        });
        viewModel.success_stop.observe(this, new Observer<StopPushEntity>() {
            @Override
            public void onChanged(StopPushEntity stopPushEntity) {
                Log.i("TAG", "onChanged: 下播成功"+stopPushEntity.getMessage());
            }
        });
        viewModel.failure_stop.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Log.i("TAG", "onChanged: 下播失败"+s);
            }
        });
    }

    @Override
    protected void initView() {
        //mPlayerView 即 step3 中添加的界面 view
        mView = (TXCloudVideoView)findViewById(R.id.video_view);
//创建 player 对象
        mLivePlayer = new V2TXLivePlayerImpl(this);
//关键 player 对象与界面 view
        mLivePlayer.setRenderView(mView);

// 指定对应的直播协议为 RTMP，该协议不支持连麦
        mLivePusher = new V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP);
        // 启动本地摄像头预览
        TXCloudVideoView mPusherView = (TXCloudVideoView) findViewById(R.id.pusher_tx_cloud_view);
        mLivePusher.setRenderView(mPusherView);
        mLivePusher.startCamera(true);
        mLivePusher.startMicrophone();


        dataBinding.startLive.setOnClickListener(view -> {
            // 根据推流协议传入对应的 URL 即可启动推流， RTMP 协议以 rtmp:// 开头，该协议不支持连麦
            startPush();
        });
        dataBinding.closeLive.setOnClickListener(view -> {
            mLivePusher.stopPush();
            stopPush();
        });
    }
    public void stopPush(){
        viewModel.stopLive();
    }
    public void startPush(){
        int ret = mLivePusher.startPush(url);
        if (ret == V2TXLIVE_ERROR_INVALID_LICENSE) {
            Log.i("sss", "startRTMPPush: license 校验失败");
        }else {
            if(TextUtils.isEmpty(dataBinding.pushEt.getText().toString())){
                Log.i("TAG", "startPush: 描述不能为空");
            }else {
                viewModel.startLive(url,dataBinding.pushEt.getText().toString());
            }
        }
    }

    @Override
    protected Class<PushViewModel> getViewModel() {
        return PushViewModel.class;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_liu;
    }
}